package com.tubeonfire.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.gdata.client.batch.BatchInterruptedException;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.batch.BatchOperationType;
import com.google.gdata.data.batch.BatchStatus;
import com.google.gdata.data.batch.BatchUtils;
import com.google.gdata.data.youtube.PlaylistEntry;
import com.google.gdata.data.youtube.PlaylistFeed;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.util.ServiceException;
import com.tubeonfire.entity.Tube;

public class YoutubeBatchService {

	private static YouTubeService service = new YouTubeService("tubeilike");
	private static final Logger log = Logger.getLogger(YoutubeService.class
			.getName());
	private static List<Tube> listResult = new ArrayList<Tube>();

	public static List<Tube> getListResult() {
		return listResult;
	}

	public static void setListResult(List<Tube> listResult) {
		YoutubeBatchService.listResult = listResult;
	}

	public static void searchByListId(List<String> tubeIds, boolean fullContent) {
		try {
			listResult = new ArrayList<Tube>();
			VideoFeed batchFeed = new VideoFeed();
			BatchUtils.setBatchOperationType(batchFeed,
					BatchOperationType.QUERY);

			for (String videoId : tubeIds) {
				VideoEntry videoEntry = new VideoEntry(
						String.format(
								"http://gdata.youtube.com/feeds/api/videos/%s",
								videoId));
				batchFeed.getEntries().add(videoEntry);
			}
			VideoFeed batchResponse;
			batchResponse = service.batch(new URL(
					"http://gdata.youtube.com/feeds/api/videos/batch"),
					batchFeed);
			for (VideoEntry videoEntry : batchResponse.getEntries()) {
				if (BatchUtils.isSuccess(videoEntry)) {
					Tube obj = YoutubeService.videoEntryToTube(videoEntry);
					if (obj != null) {
						listResult.add(obj);
					}
				}
			}
		} catch (BatchInterruptedException e) {
			log.warning(e.toString());
			e.printStackTrace();
		} catch (MalformedURLException e) {
			log.warning(e.toString());
			e.printStackTrace();
		} catch (IOException e) {
			log.warning(e.toString());
			e.printStackTrace();
		} catch (ServiceException e) {
			log.warning(e.toString());
			e.printStackTrace();
		}

	}

	public void test() throws MalformedURLException, IOException,
			ServiceException {
		// retrieve a playlist feed and do some batch operations on it
		PlaylistFeed feed = service.getFeed(new URL(""), PlaylistFeed.class);

		// retrieve the batch link from the feed
		URL batchUrl = new URL(feed.getFeedBatchLink().getHref());
		PlaylistFeed batchFeed = new PlaylistFeed();

		// delete the third playlist entry
		PlaylistEntry entryToDelete = feed.getEntries().get(2);
		BatchUtils.setBatchId(entryToDelete, "B");
		BatchUtils.setBatchOperationType(entryToDelete,
				BatchOperationType.DELETE);
		batchFeed.getEntries().add(entryToDelete);

		// add a new video to the playlist
		PlaylistEntry entryToAdd = new PlaylistEntry();
		entryToAdd.setId("mTrp-GKG9Bo");
		BatchUtils.setBatchId(entryToAdd, "A");
		BatchUtils.setBatchOperationType(entryToAdd, BatchOperationType.INSERT);
		batchFeed.getEntries().add(entryToAdd);

		// move the second entry to the top of the playlist
		PlaylistEntry entryToUpdate = feed.getEntries().get(1);
		entryToUpdate.setPosition(0);
		BatchUtils.setBatchId(entryToUpdate, "C");
		BatchUtils.setBatchOperationType(entryToUpdate,
				BatchOperationType.UPDATE);
		batchFeed.getEntries().add(entryToUpdate);

		// return the updated entry
		PlaylistEntry entryToGet = feed.getEntries().get(0);
		BatchUtils.setBatchId(entryToGet, "D");
		BatchUtils.setBatchOperationType(entryToGet, BatchOperationType.QUERY);
		batchFeed.getEntries().add(entryToGet);

		PlaylistFeed batchResponse = service.batch(batchUrl, batchFeed);

		for (PlaylistEntry entry : batchResponse.getEntries()) {
			if (BatchUtils.isSuccess(entry)) {
				System.out.println("Operation " + BatchUtils.getBatchId(entry)
						+ " succeeded!");
				String operation = BatchUtils.getBatchOperationType(entry)
						.getName();
				System.out.println("The " + operation + " worked!");
			} else {
				System.out.println("Operation " + BatchUtils.getBatchId(entry)
						+ "failed!");
				BatchStatus status = BatchUtils.getBatchStatus(entry);
				System.out.println(status.getCode() + " - "
						+ status.getContent());
			}
		}
	}
}
